<?php

namespace Admin\Controller;

class IntroController extends CommonController
{
    // 列表
    public function lists()
    {
        $key = I('request.key', '', 'trim');

        if ($key) {
            $key_type = I('request.key_type', 0, 'intval');
            if ($key_type == '1') {
                $map['title'] = array('like', '%' . $key . '%');
            }
            if ($key_type == '2') {
                $map['content'] = array('like', '%' . $key . '%');
            }
        }

        $list = D('Intro')->get_list($map, true);

        $this->assign('lists', $list);

        $this->display();
    }

    // 添加
    public function add()
    {
        if (IS_POST) {
            $data = D("Intro");
            if (!$data->create()) {
                $this->error($data->getError());
            } else {
                $data->username = session('uu.username');
                $data->add();
                $this->success('保存完成');
            }
        } else {
            $this->display();
        }
    }

    // 编辑
    public function edit()
    {
        if (IS_POST) {
            $data = D("Intro");
            if (!$data->create()) {
                $this->error($data->getError());
            } else {
                $data->save();
                $this->success('保存完成');
            }
        } else {
            $id = I('get.id', '', 'intval');
            if ($id) {
                $data      = M("Intro");
                $map['id'] = $id;
                $show      = $data->where($map)->limit(1)->find();
                $this->assign('show', $show);
                $this->display();
                //dump($show);
            } else {
                $this->error('非法请求');
            }
        }
    }

    // 删除
    public function delete()
    {
        $ids = I('request.id');
        $ids = is_array($ids) ? implode(",", $ids) : $ids;
        if ($ids && preg_match("/^(\d{1,10},)*(\d{1,10})$/", $ids)) {
            $data = M("Intro");
            $s    = $data->delete($ids);
            $this->success("成功删除{$s}条数据");
        } else {
            $this->error('请选择要删除的信息！');
        }
    }
}